package cn.ffcs.organ.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

import cn.ffcs.base.dao.ExtBaseDAO;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.organ.model.OrgRelaPerson;
import cn.ffcs.organ.model.Person;

/**
 * 
 * 人员DAO<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class PersonDAO<P extends Person> extends ExtBaseDAO<P> implements
        IPersonDAO<P>
{
	private JdbcTemplate jdbcTemplate;
    public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	public String getEntityName()
    {
        return new Person().getClass().getName();
    }
	/**通过name取userid
	 * 
	 * @param name
	 * @return
	 */
    @SuppressWarnings("unchecked")
	public String getUserId(String name){
    	String sql="select t.user_id from t_secu_user t,t_organ_person t2 where t.person=t2.id and t2.name='黄小华'";
    	List<String> list=null;
    	list=getJdbcTemplate().queryForList(sql);
    	if(list.size()!=0){
    		return list.get(0);
    	}else{
    		return null;
    	}
    }
    /**
     * 通过name取person
     * @param name
     * @return
     */
    @SuppressWarnings("unchecked")
	public Person getPersonByName(String name){
    	List<Person> list=new ArrayList<Person>();
    	String sql=" from Person t where t.name = '"+name+"'";
    	list=getHibernateTemplate().find(sql);
    	if(list.size()!=0){
    		return list.get(0);
    	}else{
    		return null;
    	}
    } 
    /*
     * 查询未与当前组织关联的所有人员
     */
    public PageControl findSelectByPage(Person person,int currPage, int pageSize,int organId){
        String hql=" from  Person a where  a.id not in (select b.person.id from  OrgRelaPerson b where b.organization.id="+organId+")";
        List list=super.getSession().createQuery("select count(*) "+hql).list();
        String result=list.get(0).toString();
        int count=Integer.parseInt(result);
        PageControl pageControl=new PageControl(count,currPage,pageSize);
        final int startRow = pageControl.getStart();
        List <Person> ls =super.getSession().createQuery(hql).setFirstResult(pageSize*(currPage-1)).setMaxResults(pageSize).list();
        return new PageControl(ls, startRow, pageSize, currPage,
                pageControl.getTotalPage(), pageControl.getTotal());
    }
}
